<template>
  <merge-customer
  :isBlight="true"
  :labelValue="6"
  :currentBusinessObj="currentBusinessObj"
  :bindCustomer="bindCustomer "
  :custInfo = "custInfo"
  :tel="tel"
  :cdrCallInterfaceBtn="true"
  @merge="MergeCust"
  @addCustomer="addCust"
  >
  </merge-customer>
</template>
<script>
  // 此组件是来电合并客户
  import MergeCustomer from '../customer/AddCustomer'
//  import customerApi from '@/store/api/customer'
  export default {
    name: 'CdrMergeCustomer',
    data () {
      return {
        currentBusinessObj: {
          type: 'customer',
          tabType: 'customer_my',
          labelValue: 6 // 默认切换到合并客户页面
        },
        bindCustomer: {  // 绑定已存在客户的数据
          searchShow: true,
          custObj: {},
          form: null
        }
      }
    },
    props: {
      id: String, // 通话id
      tel: String, // 来电号码
      field: String, // 搜索类型 eg: 名称，号码，邮件
      callProvince: String, // 来电号码的省
      callCity: String // 来电号码的市
    },
    computed: {
      custInfo () { // 来电的客户信息，eg: 号码，省市
        return {callProvince: this.callProvince, callCity: this.callCity, phone: [{tel: this.tel, memo: '', area: ''}]}
      }
    },
    components: {
      MergeCustomer
    },
    methods: {
      MergeCust (val) { // 合并客户
        let routes = this.$route.path.split('/')
        let tabType = routes[3]
        let callId = routes[4]
        if (tabType === 'cdr_call') {
          this.cdrCallMergeCustomer(val)
        } else if (tabType === 'cdr_outbound') {
          this.mergeOutboundCustomer(val, callId)
        }
      },
      addCust (form) { // 添加客户
        let _id = this.$route.params.id
        form.callId = _id
        form.dealSrc = 'popup'
        form.actionType = 'self'
        form.callTel = this.tel
        let routes = this.$route.path.split('/')
        let tabType = routes[3]
        if (tabType === 'cdr_outbound') {
          form.dealSrc = 'dialPopup'
        }
        this.$store.dispatch('callLocationCustomer2Add', form).then((val) => {
          if (tabType === 'cdr_outbound') {
            this.$store.commit('call/TEMP_CUSTOMER_DATA', {callId: form.callId, customer: val.data})
          }
          this.$emit('addCustomerSuccess', true)
        })
      },
      cdrCallMergeCustomer (val) {
        if (val.custInfo) {
          let callId = this.id
          let tel = this.tel
          if (val.custInfo.phone) {
            let exists = false
            val.custInfo.phone.forEach(function (obj) {
              if (obj.tel === tel) { // 判断客户信息中存不存在此号码，不存在的话push，存在的话跳过
                exists = true
              }
            })
            if (!exists) {
              val.custInfo.phone.push({tel: tel, memo: ''})
            }
          } else { // 有部分数据不存在phone字段
            val.custInfo.phone = [{tel: tel, memo: ''}]
          }
          let newData = {
            customerId: val.custInfo._id,
            callId: callId,
            phone: tel,
            dealSrc: 'popup'
          }
          this.$store.dispatch('callScreenLocationCustomer', newData).then(resp => {
            this.$message({
              type: 'success',
              message: this.$t('customer.succMerge')
            })
            this.$store.commit('call/UPDATE_CURRENT_CUSTOMER_INFO', {callId: callId, customer: val.custInfo})
            this.$emit('mergeCustomerSuccess', true)
          })
        }
      },
      mergeOutboundCustomer (val, callId) { // 合并客户
        let phoneArr = val.custInfo.phone
        let phoneFlag = false
        const newArr = []
        if (phoneArr) {
          for (let i = 0; i < phoneArr.length; i++) {
            if (phoneArr[i].tel === this.tel) {
              phoneFlag = true
              break
            } else {
              phoneFlag = false
            }
            if (phoneArr[i].tel) {
              newArr.push(phoneArr[i])
            }
          }
        }
        if (phoneFlag) { // 点击保存按钮之后直接合并客户
          this.mergeCustomerNow(val, callId, this.tel)
        } else { // 合并的客户没有当前号码,先把号码更新过去之后再合并
          let newPhoneObj = {tel: this.tel, memo: ''}
          // 当这个用户没有电话时，没有phone字段
          if (!val.custInfo.phone) {
            val.custInfo.phone = []
          }
          val.custInfo.phone = newArr
          val.custInfo.phone.push(newPhoneObj)
          this.updateCustomer(val, callId, this.tel)
        }
      },
      mergeCustomerNow (val, callId, phone) {
        let data = {
          customerId: val._id,
          callId: callId,
          phone: phone,
          dealSrc: 'dialPopup',
          sessionId: this.$store.state.session.sessionId
        }
        this.$store.dispatch('callScreenLocationCustomer', data).then(resp => {
          this.isDailog = false
          this.$message({
            type: 'success',
            message: this.$t('customer.succMerge')
          })
          this.$store.commit('call/UPDATE_CURRENT_CUSTOMER_INFO', {callId: data.callId, customer: val.custInfo})
          this.$store.commit('call/TEMP_CUSTOMER_DATA', {callId: data.callId, customer: val.custInfo})
          this.$emit('mergeCustomerSuccess', true)
        })
      },
      updateCustomer (val, callId, phone) { // 先更新客户再合并
        let data = val.custInfo || {}
        data.type = 'customer'
        data.tabType = 'customer_my'
        data.callId = callId
        this.$store.dispatch('updateCustomer', data).then((resp) => {
          if (!resp) {
            return
          }
          this.mergeCustomerNow(val, callId, phone)
        })
      }
    },
    beforeMount () {
      this.bindCustomer.form = {'field': 'phone', 'combox': this.tel, 'page': 1, 'limit': 10, 'custType': 'call'}
    }
  }
</script>
